package com.weilaiwd.admin.web;

import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Page;
import com.weilaiwd.admin.constant.Sql;
import com.weilaiwd.common.model.Article;
import com.weilaiwd.common.model.Channel;
import com.weilaiwd.common.web.BaseAction;

public class ArticleAction extends BaseAction {
	
	public void index() {
		List<Channel> channels = Channel.dao.find(Sql.Channel.findAll);
		JSONArray array = new JSONArray();
		for(Channel c:channels) {
			JSONObject json = new JSONObject();
			json.put("id", c.getInt("id"));
			json.put("pId", c.getInt("pid"));
			json.put("name", c.getStr("name"));
			array.add(json);
		}
		setAttr("treeJson", array.toString());
	}
	
	public void table() {
		int start = getParaToInt("iDisplayStart");
		int channelId = getParaToInt("cid");
		int pageSize = 2;
		int pageNo = start/pageSize+1;
		Page<Article> page = Article.dao.paginate(pageNo, pageSize, "select *", Sql.Article.getArticlesByChannelId, channelId);
		
		JSONObject json = new JSONObject();
		json.put("sEcho", getPara("sEcho"));  
		json.put("iTotalRecords", page.getTotalRow());  
		json.put("iTotalDisplayRecords", page.getTotalRow());  
		json.put("aaData", page.getList());
		
		renderJson(json);
	}
	
	public void toAdd() {
		renderJsp("add.jsp");
	}

}
